use "${ddata}cleaned_data_wACSweight.dta", clear
/* App Table 3*/


gen rec_rejectoffer = qgn0j_1 if male == 0
replace rec_rejectoffer = qgn0j_2 if male == 1
forval x=1/5 {
	gen rec_timeoff`x' = qgn2newb_`x' if male==1
	replace rec_timeoff`x' = qgn2newa_`x' if male==0
}

gen female = 1 - male

gen young_children_binary = .
replace young_children_binary = 1 if residence_children_under6 > 0 & residence_children_under6!=.
replace young_children_binary = 0 if residence_children_under6 == 0

gen young_children_binary2 = .
replace young_children_binary2 = 1 if residence_children_under18 > 0 & residence_children_under18 !=.
replace young_children_binary2 = 0 if residence_children_under18 == 0

gen South = current_region == "region_south"
gen Midwest = current_region == "region_midwest"
gen Northeast = current_region == "region_northeast"
gen West = current_region == "region_west"

cap drop working_ft
gen working_ft = q10_1==1 
replace working_ft = . if q10_1==.

gen employed = (q10_1==1 | q10_2 == 1 | q10_5 == 1)
replace employed = . if q10_1==. & q10_2==. & q10_5==.

gen unemployed = (q10_3==1 | q10_4 == 1)
replace unemployed = . if q10_3==. & q10_4==.
replace unemployed = 0 if employed == 1

gen OOWF = (q10_6 == 1 | q10_7 == 1 | q10_8 == 1 | q10_9 == 1 | q10_10 == 1)
replace OOWF = . if (q10_6 == . & q10_7 == . & q10_8 == . & q10_9 == . & q10_10 == .)
replace OOWF = 0 if employed == 1 | unemployed == 1



local variables female white age college married young_children_binary young_children_binary2 working_ft employed unemployed OOWF ///
	hh_income_median Midwest Northeast South West qgn6_1 qgn6_2 qgn6_3 qgn6_4 qgn6_5 ///
	qgn0j_1 qgn0j_2 qgn2newa_1 qgn2newa_2 qgn2newa_3 qgn2newa_4 qgn2newa_5 ///
	qgn2newb_1 qgn2newb_2 qgn2newb_3 qgn2newb_4 qgn2newb_5 ///
	rec_rejectoffer rec_timeoff1 rec_timeoff2 rec_timeoff3 rec_timeoff4 rec_timeoff5

local labels "Female" "White" "Age" "College" "Married" "Has Child(ren) Under Age 6" "Has Child(ren) Under Age 18" ///
	"Working FT" "Employed" "Unemployed" "Out of Labor Force" "HH Income (in \textdollar1000s)" "Midwest" "Northeast" "South" "West" ///
	"Working mother can establish warm relationship as non-working mother" ///
	"A pre-school child is likely to suffer if mother works" ///
	"It is better if man is achiever and woman takes care of home" ///
	"It is more important for wife to help husband's career than her own" ///
	"If a woman earns more than her husband it will cause problems" ///
	"Second-Order: Females in your state rec. reject offer" ///
	"Second-Order: Males in your state rec. reject offer" ///
	"Second Order: Females rec. mother take time off if earns 15 pct less" ///
	"Second Order: Females rec. mother take time off if earns same" ///
	"Second Order: Females rec. mother take time off if earns 15 pct more" ///
	"Second Order: Females rec. mother take time off if earns 30 pct more" ///
	"Second Order: Females rec. mother take time off if earns 50 pct more" ///
	"Second Order: Males rec. mother take time off if earns 15 pct less" ///
	"Second Order: Males rec. mother take time off if earns same" ///
	"Second Order: Males rec. mother take time off if earns 15 pct more" ///
	"Second Order: Males rec. mother take time off if earns 30 pct more" ///
	"Second Order: Males rec. mother take time off if earns 50 pct more" ///
	"Rec. reject offer" ///
	"Rec. mother takes time off if earns 15 pct less" ///
	"Rec. mother takes time off if earns same" ///
	"Rec. mother takes time off if earns 15 pct more" ///
	"Rec. mother takes time off if earns 30 pct more" ///
	"Rec. mother takes time off if earns 50 pct more"


local c = 0

*Create outfile
cap file close out
file open out using "${o}tmp.txt", write replace
file write out "dem|i1|i2|s1|s2|s3" _n

*Loop through demographics
foreach x in `variables' {

	*Counter
	local ++c

	*Label
	local label: word `c' of "`labels'"

	*Write out label
	file write out "`label'"

	*Define conditions
	local con1 control == 1 
	local con2 treatment == 1		

	*Regressions
	sum `x' [aw=weight_ACS] if `con1'
	loc cm = `r(mean)'
	loc csd = `r(sd)'
	sum `x' [aw=weight_ACS] if `con2'
	loc tm = `r(mean)'
	loc tsd = `r(sd)'
	reg `x' treatment [aw=weight_ACS], r
	test treatment = 0								
	loc ds = `r(p)'
	file write out "|`cm'|`csd'|`tm'|`tsd'|`ds'"	

	*Close row
	file write out _n		
}


*Bring in results
cap file close out
insheet using  "${o}tmp.txt", delim("|") names clear
erase  "${o}tmp.txt"

*Clean numbers
*replace dem = subinstr(dem, "(`ds'", "(\ds", .)
tostring *, replace force format(%3.2f)



*Export
local custom "&\multicolumn{2}{c}{Control}&\multicolumn{2}{c}{Treatment}&" 
local custom "`custom' \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5}  & {Mean}&{Std}&{Mean}&{Std}&{p-val}" 

*Export
texsave * using "${o}AppTable3.tex", replace ///
	nofix frag align(lrrrrr) headerlines("`custom'") nonames ///
	size(scriptsize)  ///
	width("6in")
filefilter "${o}AppTable3.tex" "${o}AppTable32.tex", from("\BSbegin{table}[tbp] \BScentering") to("\BScentering") replace
filefilter "${o}AppTable32.tex" "${o}AppTable3.tex", from("\BSend{table}") to("") replace
erase "${o}AppTable32.tex"
